package com.ljd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.entity.*;
import com.ljd.entity.frontVo.CourseFrontVo;
import com.ljd.entity.frontVo.CourseWebVo;
import com.ljd.entity.page.PageResponse;
import com.ljd.entity.vo.CourseInfoVo;
import com.ljd.entity.vo.CoursePublishVo;
import com.ljd.mapper.EduCourseMapper;
import com.ljd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.handler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2021-12-10
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduVideoService videoService;

    //添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0) {
            //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        //获取添加之后课程id
        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        boolean save = this.updateById(eduCourse);
        if (!save) {
            throw new GuliException(20001,"修改课程信息失败");
        }
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(eduCourse.getId());
        courseDescriptionService.updateById(description);
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        if(courseId.isEmpty()) {
            return new CourseInfoVo();
        }
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        EduCourseDescription description = courseDescriptionService.getById(eduCourse.getId());
        BeanUtils.copyProperties(description, courseInfoVo);
        return courseInfoVo;
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        if(publishCourseInfo == null){
            return new CoursePublishVo();
        }
        return publishCourseInfo;
    }

    @Override
    public void publishCourse(String courseId) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",courseId);
        EduCourse eduCourse = baseMapper.selectOne(wrapper);
        if(eduCourse==null){
            return;
        }
        if("Normal".equals(eduCourse.getStatus())) {
            return;
        }
        eduCourse.setStatus("Normal");
        baseMapper.updateById(eduCourse);
    }

    @Override
    public PageResponse<EduCourse> listByCondition(Integer current, Integer pageSize, String title, String status) {
        Page<EduCourse> page = new Page<>(current, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        wrapper.orderByDesc("gmt_modified");
        IPage<EduCourse> selectPage = baseMapper.selectPage(page, wrapper);
        PageResponse<EduCourse> response = new PageResponse<>();
        response.setItems(selectPage.getRecords());
        response.setCurrent((int)selectPage.getCurrent());
        response.setPageSize((int)selectPage.getSize());
        response.setTotal((int)selectPage.getTotal());
        return response;
    }

    @Override
    public void deleteCourse(String courseId) {
        // 先删关系
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        // 课程描述
        courseDescriptionService.removeById(courseId);
        // 课程章节
        chapterService.remove(wrapper);
        // 课程小节和视频
        videoService.removeBatch(courseId);

        boolean b = this.removeById(courseId);
        if(!b){
            throw new GuliException(20001,"无法删除");
        }
    }


    @Override
    public Map<String, Object> getCourseByCondition(Long current, Long pageSize, CourseFrontVo courseFrontVo) {
        Page<EduCourse> coursePage = new Page<>(current, pageSize);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){ // 课程一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){ // 二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        baseMapper.selectPage(coursePage,wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("items",coursePage.getRecords());
        map.put("current",coursePage.getCurrent());
        map.put("pageSize",coursePage.getSize());
        map.put("pages",coursePage.getPages());
        map.put("total",coursePage.getTotal());
        map.put("hasNext",coursePage.hasNext());
        map.put("hasPrevious",coursePage.hasPrevious());

        return map;
    }

    @Override
    public CourseWebVo getCourseWebVoById(String courseId) {
        CourseWebVo courseWebVo = baseMapper.getCourseWebVoById(courseId);
        return courseWebVo;
    }
}
